package com.hoperun.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.hoperun.bean.AppUserGroups;
import com.hoperun.dao.AppUserGroupsDAO;
import com.hoperun.framework.ibatis.page.BootGrid;
import com.hoperun.framework.ibatis.page.BootGridPage;
import com.hoperun.framework.query.Criteria;
import com.hoperun.service.IAppUserGroupsSV;
import com.hoperun.utils.Constant;

@Service("appUserGroupsSV")
public class AppUserGroupsSVImpl implements IAppUserGroupsSV {
	@Resource
	private AppUserGroupsDAO appUserGroupsDAO;

	@Override
	public void delete(long id) throws Exception {
		appUserGroupsDAO.delete(id);
	}

	@Override
	public AppUserGroups queryById(long id) throws Exception {
		return appUserGroupsDAO.queryById(id);
	}

	@Override
	public AppUserGroups save(AppUserGroups appUserGroups) throws Exception {
		appUserGroupsDAO.insert(appUserGroups);
		return appUserGroups;
	}

	@Override
	public void update(AppUserGroups appUserGroups) throws Exception {
		appUserGroupsDAO.update(appUserGroups);
	}

	@Override
	public void saveOrUpdate(AppUserGroups appUserGroups) throws Exception {
		if (appUserGroups == null)
			return;
		if (null != appUserGroups.getGid() && appUserGroups.getGid() > 0) {
			update(appUserGroups);
		} else {
			save(appUserGroups);
		}
	}

	@Override
	public void saveOrUpdate(List<AppUserGroups> list) throws Exception {
		if (list != null && list.size() > 0) {
			for (AppUserGroups appUserGroups : list) {
				saveOrUpdate(appUserGroups);
			}
		}
	}

	@Override
	public List<AppUserGroups> queryByCondition(Criteria criteria)
			throws Exception {
		return appUserGroupsDAO.queryByCondition(criteria);
	}

	@Override
	public int queryCountByCondition(Criteria criteria) throws Exception {
		return appUserGroupsDAO.queryCountByCondition(criteria);
	}

	@Override
	public BootGridPage<AppUserGroups> queryPage(Criteria criteria,
			BootGrid<AppUserGroups> grid) throws Exception {

		// AppUserGroups model = grid.getEntity();

		int totalCount = appUserGroupsDAO.queryCountByCondition(criteria);

		if (0 < totalCount) {

			Map<String, String> sorts = grid.getSort();

			/** 设置排序规则 **/
			if (null != sorts && sorts.size() > Constant.ZERO) {
				Set<String> keySets = sorts.keySet();
				String sort = "";
				for (String key : keySets) {
					sort += " , " + key + " " + sorts.get(key);
				}
				criteria.orderBy(sort.replaceFirst(",", ""));
			}
			if (grid.getCurrent() >= Constant.ZERO) {
				int start = (grid.getCurrent() - Constant.ONE)
						* grid.getRowCount();
				int size = grid.getRowCount();

				criteria.limit(start, size);
			}
			List<AppUserGroups> list = appUserGroupsDAO
					.queryByCondition(criteria);
			BootGridPage<AppUserGroups> page = new BootGridPage<AppUserGroups>(
					grid.getCurrent(), grid.getRowCount(), totalCount, list);

			return page;
		}
		return new BootGridPage<AppUserGroups>(grid.getCurrent(),
				grid.getRowCount(), totalCount, new ArrayList<AppUserGroups>());

	}
}
